// miniprogram/pages/home/register/register.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    msg: "正在加载",
    inputValue: ""
  },
  /**
   * 表单
   */
  formSubmit: function(e) {
    var that = this;
    var msg = "";
    let {
      schoolName,
      name,
      stuNo,
      password
    } = e.detail.value;
    console.log(schoolName, name, stuNo, password)
    if (!schoolName || !name || !stuNo || !password) {
      if (!schoolName) {
        msg = msg + "学校 "
      }
      if (!name) {
        msg = msg + "姓名 "
      }
      if (!stuNo) {
        msg = msg + "学号 "
      }
      if (!password) {
        msg = msg + "密码 "
      }
      this.setData({
        msg: msg + "不能为空"
      });
      this.setData({
        showTopTips: true
      });
      setTimeout(function() {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (stuNo.length != 10 || password.length < 6) {
      if (stuNo.length != 10) {
        msg = msg + "学号 "
      }
      if (password.length < 6) {
        msg = msg + "密码 "
      }
      this.setData({
        msg: msg + "长度出错，请重新输入"
      });
      this.setData({
        showTopTips: true,
      });
      setTimeout(function() {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      wx.showLoading({
        title: that.data.msg,
      })
      wx.request({
        url: app.globalData.url + '/user/register',
        data: {
          openid: app.globalData.openid,
          nickName: app.globalData.userInfo.nickName,
          avatarUrl: app.globalData.userInfo.avatarUrl,
          schoolName: schoolName,
          name: name,
          stuNo: stuNo,
          password: password
        },
        success: res => {
          var code = res.data.code;
          if (code == 1) {
            wx.hideLoading()
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 1000,
              mask: true
            })
            wx.switchTab({
              url: '../index/index',
            })
          } else { //绑定失败
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              success: function(res) {
                if (res.confirm) {
                  that.setData({
                    inputValue: ""
                  })
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})